<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="吐槽一句微信的api真的是坑  参考文章:https:&#x2F;&#x2F;juejin.im&#x2F;post&#x2F;5833975961ff4b006c249067 支付宝支付​    1. 合理的支付流程">
<meta property="og:type" content="article">
<meta property="og:title" content="快速集成支付宝支付和微信支付">
<meta property="og:url" content="http://yoursite.com/2017/08/26/androidpay/index.html">
<meta property="og:site_name" content="二松同学">
<meta property="og:description" content="吐槽一句微信的api真的是坑  参考文章:https:&#x2F;&#x2F;juejin.im&#x2F;post&#x2F;5833975961ff4b006c249067 支付宝支付​    1. 合理的支付流程">
<meta property="og:image" content="https://gw.alipayobjects.com/zos/skylark/public/files/c0c28dbec662e7880d06c6d0232cf030.png">
<meta property="article:published_time" content="2017-08-26T05:56:56.000Z">
<meta property="article:modified_time" content="2020-06-23T03:08:32.947Z">
<meta property="article:author" content="二松">
<meta property="article:tag" content="android">
<meta property="article:tag" content="支付">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gw.alipayobjects.com/zos/skylark/public/files/c0c28dbec662e7880d06c6d0232cf030.png">

<link rel="canonical" href="http://yoursite.com/2017/08/26/androidpay/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>快速集成支付宝支付和微信支付 | 二松同学</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">二松同学</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">拥有梦想只是一种智力，实现梦想才是一种能力</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/08/26/androidpay/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="二松">
      <meta itemprop="description" content="good good study day day up.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="二松同学">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          快速集成支付宝支付和微信支付
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-08-26 13:56:56" itemprop="dateCreated datePublished" datetime="2017-08-26T13:56:56+08:00">2017-08-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-23 11:08:32" itemprop="dateModified" datetime="2020-06-23T11:08:32+08:00">2020-06-23</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E5%8D%9A%E5%AE%A2/" itemprop="url" rel="index"><span itemprop="name">技术博客</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>吐槽一句微信的api真的是坑</p>
</blockquote>
<p>参考文章:<a href="https://juejin.im/post/5833975961ff4b006c249067" target="_blank" rel="noopener">https://juejin.im/post/5833975961ff4b006c249067</a></p>
<h2 id="支付宝支付"><a href="#支付宝支付" class="headerlink" title="支付宝支付"></a>支付宝支付</h2><p>​    1. 合理的支付流程</p>
<p><img src="https://gw.alipayobjects.com/zos/skylark/public/files/c0c28dbec662e7880d06c6d0232cf030.png" alt="支付宝流程"></p>
<ol>
<li><p>导入sdk</p>
<ul>
<li>从官网下载jar包放在libs文件夹下<code>alipaySdk-20170725.jar</code></li>
<li>manifest.xml下添加权限</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.INTERNET"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.ACCESS_NETWORK_STATE"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.ACCESS_WIFI_STATE"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.READ_PHONE_STATE"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>manifest.xml下添加activity支付宝入口</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--支付宝activity,当没有安装支付宝时打开--&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">activity</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:name</span>=<span class="string">"com.alipay.sdk.app.H5PayActivity"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:configChanges</span>=<span class="string">"orientation|keyboardHidden|navigation|screenSize"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:exported</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:screenOrientation</span>=<span class="string">"behind"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:windowSoftInputMode</span>=<span class="string">"adjustResize|stateHidden"</span> &gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">activity</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:name</span>=<span class="string">"com.alipay.sdk.app.H5AuthActivity"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:configChanges</span>=<span class="string">"orientation|keyboardHidden|navigation"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:exported</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:screenOrientation</span>=<span class="string">"behind"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">android:windowSoftInputMode</span>=<span class="string">"adjustResize|stateHidden"</span> &gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>发起<strong>支付</strong></p>
<blockquote>
<p>这里的订单(orderInfo)又服务端生成,我们负责调用支付宝app</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 支付宝支付 必须在异步线程</span></span><br><span class="line"><span class="comment">    *</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> orderInfo app支付请求参数字符串，主要包含商户的订单信息，</span></span><br><span class="line"><span class="comment">    *                  key=value形式，以&amp;连接,由服务端生成</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">alipay</span><span class="params">(<span class="keyword">final</span> String orderInfo)</span> </span>&#123;</span><br><span class="line">       Runnable payRunnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line"></span><br><span class="line">           <span class="meta">@Override</span></span><br><span class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">               PayTask alipay = <span class="keyword">new</span> PayTask(MainActivity.<span class="keyword">this</span>);</span><br><span class="line">               <span class="comment">//传入支付订单信息,设置ture表示显示支付的loading</span></span><br><span class="line">               Map&lt;String, String&gt; result = alipay.payV2(orderInfo, <span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">               Message msg = <span class="keyword">new</span> Message();</span><br><span class="line">               msg.what = ALI_PAY_FLAG;</span><br><span class="line">               msg.obj = result;</span><br><span class="line">               handler.sendMessage(msg);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;;</span><br><span class="line"></span><br><span class="line">       Thread payThread = <span class="keyword">new</span> Thread(payRunnable);</span><br><span class="line">       payThread.start();</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>处理<strong>结果</strong></p>
<blockquote>
<p>这里处理的是支付宝支付的结果,完成支付后还应该请求服务器,同步服务器结果</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> Handler handler = <span class="keyword">new</span> Handler() &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleMessage</span><span class="params">(Message msg)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">                <span class="keyword">case</span> ALI_PAY_FLAG: &#123;</span><br><span class="line">                    <span class="comment">//PayResult非常简单的工具类,把map里的结果取出来(来自支付宝demo)</span></span><br><span class="line">                    PayResult payResult = <span class="keyword">new</span> PayResult((Map&lt;String, String&gt;) msg.obj);</span><br><span class="line">                    </span><br><span class="line">                    <span class="comment">//对于支付结果，请商户依赖服务端的异步通知结果</span></span><br><span class="line">                  	<span class="comment">//同步通知结果，仅作为支付结束的通知</span></span><br><span class="line">                    String resultInfo = payResult.getResult();<span class="comment">// 同步返回需要验证的信息</span></span><br><span class="line">                    String resultStatus = payResult.getResultStatus();<span class="comment">//状态码</span></span><br><span class="line">                    String memo = payResult.getMemo();<span class="comment">//附加信息,如果不为空可以提示该内容</span></span><br><span class="line">                    <span class="keyword">switch</span> (resultStatus) &#123;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"9000"</span>:</span><br><span class="line">                           <span class="comment">//支付成功</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"8000"</span>:</span><br><span class="line">                           <span class="comment">//支付结果确认中</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"4000"</span>:</span><br><span class="line">                            <span class="comment">//订单支付失败</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"5000"</span>:</span><br><span class="line">                          	<span class="comment">//重复请求</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"6001"</span>:</span><br><span class="line">                           <span class="comment">//用户中途取消</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"6002"</span>:</span><br><span class="line">                           <span class="comment">//网络连接出错</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> <span class="string">"6004"</span>:</span><br><span class="line">                            <span class="comment">//支付结果未知,请查询订单</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">default</span>:</span><br><span class="line">                           <span class="comment">//其它支付错误</span></span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    Log.e(<span class="string">"TAG"</span>, <span class="string">"handleMessage: "</span> + payResult.toString());</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">default</span>:</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="微信支付"><a href="#微信支付" class="headerlink" title="微信支付"></a>微信支付</h2><blockquote>
<p>注意:微信支付需要签名和申请的时候填入的签名一直才能成功</p>
<p>ps:第一次调用可以成功,就算没有签名没有申请支付功能也能成功,应该是微信的小心思</p>
</blockquote>
<ol>
<li><p>导入sdk</p>
<ul>
<li>从官网下载jar包放在libs文件夹下<code>libammsdk.jar</code></li>
<li>manifest.xml下添加权限</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;uses-permission android:name=<span class="string">"android.permission.INTERNET"</span> /&gt;</span><br><span class="line">&lt;uses-permission android:name=<span class="string">"android.permission.MODIFY_AUDIO_SETTINGS"</span>/&gt;</span><br><span class="line">&lt;uses-permission android:name=<span class="string">"android.permission.WRITE_EXTERNAL_STORAGE"</span>/&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>manifest.xml下添加activity微信支付入口</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 微信支付 --&gt;</span><br><span class="line">&lt;activity</span><br><span class="line">android:name=<span class="string">".wxapi.WXPayEntryActivity"</span></span><br><span class="line">android:exported=<span class="string">"true"</span></span><br><span class="line">android:launchMode=<span class="string">"singleTop"</span>/&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>从官方sdk中拷贝<code>WXPayEntryActivity</code>到项目中</li>
</ul>
<blockquote>
<p>拷贝的路径是<code>你的包名/wxapi/WXPayEntryActivity</code>,可以测试下是否能调起界面</p>
</blockquote>
</li>
<li><p>配置<code>WXPayEntryActivity</code></p>
<ul>
<li>onCreate</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line"></span><br><span class="line"><span class="comment">//		去除界面直接跳转,也可以定义</span></span><br><span class="line"><span class="comment">//		setContentView();</span></span><br><span class="line"><span class="comment">//		传入自己的appid</span></span><br><span class="line">    	api = WXAPIFactory.createWXAPI(<span class="keyword">this</span>, Constants.APP_ID);</span><br><span class="line">        api.handleIntent(getIntent(), <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>重写<code>onResp()</code>根据成功失败做出相应操作</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onResp</span><span class="params">(BaseResp resp)</span> </span>&#123;</span><br><span class="line">	Log.e(TAG, <span class="string">"onPayFinish, errCode = "</span> + resp.errCode);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//判断状态</span></span><br><span class="line">	<span class="keyword">if</span> (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) &#123;</span><br><span class="line">            <span class="keyword">if</span> (resp.errCode == <span class="number">0</span>) &#123;</span><br><span class="line">                Toast.makeText(<span class="keyword">this</span>, <span class="string">"支付成功"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (resp.errCode == -<span class="number">2</span>) &#123;</span><br><span class="line">                Toast.makeText(<span class="keyword">this</span>, <span class="string">"用户取消"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;<span class="comment">//-1的状态</span></span><br><span class="line">                Toast.makeText(<span class="keyword">this</span>, <span class="string">"支付失败,请重试"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//记得关闭</span></span><br><span class="line">        finish();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>发起<code>请求</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 微信支付</span></span><br><span class="line"><span class="comment">    *</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> data 接口返回的订单信息,是一个json</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">weixinPay</span><span class="params">(String data)</span> </span>&#123;</span><br><span class="line">       <span class="comment">// 通过WXAPIFactory工厂，获取IWXAPI的实例,记得修改为自己的appid</span></span><br><span class="line">       IWXAPI api = WXAPIFactory.createWXAPI(<span class="keyword">this</span>, Constants.APP_ID);</span><br><span class="line">       <span class="comment">// 将该app注册到微信</span></span><br><span class="line">       api.registerApp(Constants.APP_ID);</span><br><span class="line">       Log.e(<span class="string">"TAG"</span>, <span class="string">"weixinPay: "</span> + data);</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           JSONObject json = <span class="keyword">new</span> JSONObject(data);</span><br><span class="line">           PayReq req = <span class="keyword">new</span> PayReq();</span><br><span class="line">           req.appId = json.getString(<span class="string">"appid"</span>);</span><br><span class="line">           req.partnerId = json.getString(<span class="string">"partnerid"</span>);</span><br><span class="line">           req.prepayId = json.getString(<span class="string">"prepayid"</span>);</span><br><span class="line">           req.nonceStr = json.getString(<span class="string">"noncestr"</span>);</span><br><span class="line">           req.timeStamp = json.getString(<span class="string">"timestamp"</span>);</span><br><span class="line">           req.packageValue = json.getString(<span class="string">"package"</span>);</span><br><span class="line">           req.sign = json.getString(<span class="string">"sign"</span>);</span><br><span class="line">           req.extData = <span class="string">"app data"</span>; <span class="comment">// optional</span></span><br><span class="line">           <span class="comment">// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信</span></span><br><span class="line">           <span class="keyword">boolean</span> b = api.sendReq(req);</span><br><span class="line">           Log.e(<span class="string">"TAG"</span>, <span class="string">"weixinPay: "</span> + (b ? <span class="string">"true"</span> : <span class="string">"false"</span>));</span><br><span class="line">       &#125; <span class="keyword">catch</span> (JSONException e) &#123;</span><br><span class="line">           e.printStackTrace();</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>​</p>
<p>​</p>
</li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/android/" rel="tag"># android</a>
              <a href="/blog/tags/%E6%94%AF%E4%BB%98/" rel="tag"># 支付</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2017/08/25/gradle/" rel="prev" title="gradle">
      <i class="fa fa-chevron-left"></i> gradle
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2017/09/25/RoundedBitmapDrawable/" rel="next" title="RoundedBitmapDrawable剪裁">
      RoundedBitmapDrawable剪裁 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#支付宝支付"><span class="nav-number">1.</span> <span class="nav-text">支付宝支付</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#微信支付"><span class="nav-number">2.</span> <span class="nav-text">微信支付</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="二松"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">二松</p>
  <div class="site-description" itemprop="description">good good study day day up.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">27</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">二松</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  













<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
